/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.model.powersearch;



import idea.model.qnrlibrary.CustomContentItemSearchView;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JLabel;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.swing.outline.Outline;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;


/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//idea.model.powersearch//CCISearchResults//EN" ,
        autostore = false )
@TopComponent.Description(
        preferredID = "CCISearchResultsTopComponent" ,
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS )
@TopComponent.Registration( mode = "editor" , openAtStartup = false )
@ActionID( category = "Window" , id = "idea.model.powersearch.CCISearchResultsTopComponent" )
//@ActionReferences( 
//{
//    @ActionReference( path = "Menu/Window/Search Results" , position = 533 ) ,
//    @ActionReference( path = "Menu/File/Search Results" , position = 533 )
//} )
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_CCISearchResultsAction" ,
        preferredID = "CCISearchResultsTopComponent" )
@Messages(
        {
    "CTL_CCISearchResultsAction=CCISearchResults" ,
    "CTL_CCISearchResultsTopComponent=Search Results Window" ,
    "HINT_CCISearchResultsTopComponent=This is a Search Results window"
} )
public final class CCISearchResultsTopComponent extends TopComponent implements ExplorerManager.Provider
{

    private static ExplorerManager exMgr = new ExplorerManager();
    
    public CCISearchResultsTopComponent()
    {
        initComponents();
        setName( Bundle.CTL_CCISearchResultsTopComponent() );
        setToolTipText( Bundle.HINT_CCISearchResultsTopComponent() );

        setLayout( new BorderLayout() );
        
        associateLookup( ExplorerUtils.createLookup( exMgr , getActionMap() ));
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        lblCCISearchResults = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        beanTreeView2 = new org.openide.explorer.view.BeanTreeView();

        lblCCISearchResults.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblCCISearchResults, org.openide.util.NbBundle.getMessage(CCISearchResultsTopComponent.class, "CCISearchResultsTopComponent.lblCCISearchResults.text")); // NOI18N

        jScrollPane1.setViewportView(beanTreeView2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCCISearchResults)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCCISearchResults)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openide.explorer.view.BeanTreeView beanTreeView2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCCISearchResults;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened()
    {
        // TODO add custom code on component opening
    }


    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }


    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version" , "1.0" );
        // TODO store your settings
    }


    void readProperties( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }


    @Override
    public ExplorerManager getExplorerManager()
    {
        return exMgr;
    }
    
    public void displayCCISearchResults( List<CustomContentItemSearchView> resultList) {

        ExplorerManager exm =  getExplorerManager();
        Children kids = Children.create( new CCISearchResultsChildFactory( resultList ), true );
        Node rootNode = new CCISearchResultsRootNode( kids );
        OutlineView ov = new OutlineView("Survey: Ref Period");
        
        ov.setPropertyColumns(
                "questionnairesTitle", "Questionnaire Title",
                "subTitle", "Questionnaire Subtitle",
                "formDisplayNumber", "Form Display Number",
                "formNumber", "Form Number",
                "category",  "Question Category",
                "questionTitle", "Question Title",
                "version", "Question Version",
                "itemWording", "Item Wording");
        
        Outline o = ov.getOutline();
        o.setRootVisible( false );
        o.getColumnModel().getColumn(0).setMaxWidth( 15 );
        
        // prepare descriptive label at top of display
        //String labelText = lblCCISearchResults.getText();
        int size = resultList.size();
        String res = (size == 1 ? size + " result" : size + " results");
        String labelText = "Power Search Results ";
        lblCCISearchResults.setText(labelText + " -- " + res);
        add(lblCCISearchResults, BorderLayout.NORTH );
        
        add( ov, BorderLayout.CENTER);
        exm.setRootContext( rootNode );
        
    }
    
    
    
//public void displaySameContentQuestions(String survey, String refPeriod, String category, String questionTitle ) {
//        List<CustomContentItemSearchView> resultList = QueryManager.getSameContentQuestions( survey , refPeriod , category , questionTitle );
//        displayCCISearchResults(resultList);
////        ExplorerManager exm =  getExplorerManager();
////        Children kids = Children.create( new CCISearchResultsChildFactory( resultList ), true );
////        Node rootNode = new CCISearchResultsRootNode( kids );
////        OutlineView ov = new OutlineView("Survey: Ref Period");
////        
////        ov.setPropertyColumns(
////                "questionnairesTitle", "Questionnaire Title",
////                "subTitle", "Questionnaire Subtitle",
////                "formDisplayNumber", "Form Display Number",
////                "formNumber", "Form Number",
////                "category",  "Question Category",
////                "questionTitle", "Question Title",
////                "itemWording", "Item Wording");
////        
////        Outline o = ov.getOutline();
////        o.setRootVisible( false );
////        o.getColumnModel().getColumn(0).setMaxWidth( 15 );
////        add(lblCCISearchResults, BorderLayout.NORTH );
////        add( ov, BorderLayout.CENTER);
////        exm.setRootContext( rootNode );
//        
//    }


    public JLabel getLblCCISearchResults()
    {
        return lblCCISearchResults;
    }

}
